<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\api\controller;

use app\api\model\SmsLog;
use app\BaseController;
use app\api\model\User;
use app\common\model\Sms;
use Firebase\JWT\JWT;

/**
 * Class Login
 * @package app\admin\controller
 */
class Login extends BaseController
{
    use \app\common\traits\JumpTrait;
    /**
     * 初始化方法
     */
//    public function initialize()
//    {
//        parent::initialize();
//
//    }

    /**
     * 用户登录
     * @return string
     * @throws \Exception
     */
    public function index()
    {
            $post = $this->request->post();
            $rule = [
                'phone|用户名'      => 'require',
                'code|密码'       => 'require|integer',
            ];
            $this->validate($post, $rule);

            $sms = SmsLog::where('phone', $post['phone'])->order('id', 'desc')->find();
            if ($post['code']!= $sms['code']) {
                $this->error('验证码输入有误');
            }
            if ($sms['expire'] < date('Y-m-d H:i:s') || $sms['status'] == 0) {
                $this->error('验证码已超时');
            }
            $user = User::where(['phone' => $post['phone']])->find();
            if (empty($user)) {
                $user = new User();
                $user->phone = $post['phone'];
                $user->nickname = getStr(5);
                $res = $user->save();
                $user = User::where('phone', $post['phone'])->find();
            }
            $sms->status = 0;
            $sms->save();
            $user = $user->toArray();
            unset($user['password']);
            $key = "huihua";
            $payload = array(
                "iss" => "http://685.org",
                "aud" => "http://685.com",
                "iat" => time(),
                "exp" => time()+7200,
                "user" => $user
            );
            $jwt = JWT::encode($payload,$key);
            $data['msg'] = '登录成功';
            $data['token'] = $jwt;
            $this->success('登录成功', $data);
    }

    public function checkCode($phone, $code){
        
    }
    public function register(){
        $post = $this->request->post();
        $rule = [
            'phone|用户名'      => 'require',
            'password|密码'       => 'require',
        ];
        $this->validate($post, $rule);
        $user = User::where(['phone' => $post['phone']])->find();
        if (!empty($user)) {
            $this->error('用户已存在');
        }
    }

    public function sendCode(){
        $post = $this->request->post();
        $code = rand(1111,9999);
        $res = Sms::send_sms(['phone'=>$post['phone'],'param'=>json_encode(['code'=>$code])]);
//        var_dump($res);
//        die;
        if($res['Code'] == 'ok'){
            $smsLog = new SmsLog();
            $smsLog->phone = $post['phone'];
            $smsLog->code = $code;
            $smsLog->expire = date('Y-m-d H:i:s',time()+300);
            $smsLog->save();
            $this->success('发送成功');
        }else{
            $this->error('发送失败');
        }
    }
    /**
     * 用户退出
     * @return mixed
     */

    public function logout()
    {

    }

}
